package top.ljlgithub.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import top.ljlgithub.domain.PageBean;
import top.ljlgithub.domain.PageConditionSql;
import top.ljlgithub.domain.QueryCondition;
import top.ljlgithub.domain.User;
import top.ljlgithub.service.AdminService;
import top.ljlgithub.service.UserService;
import top.ljlgithub.vo.LoginParam;
import top.ljlgithub.vo.PageParam;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/3/20 11:51
 * @Version 1.0
 **/
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    AdminService adminService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(PageConditionSql pageConditionSql, QueryCondition queryCondition,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        //如果controller中有形参，但前端未传递参数，那么属性是默认值0
        if (pageConditionSql.getCurrentPage() == 0){
            pageConditionSql.setCurrentPage(1);
        }
        if (pageConditionSql.getRows() == 0){
            pageConditionSql.setRows(5);
        }
        //如果前端没有传递相应的参数，那么后端的形参会为null吗？
        //不会，因为后端接收参数时，会创建该对象作为形参，来接收参数
        //所以对象不是空的；
        //System.out.println(queryCondition == null);
        pageConditionSql.setCondition(queryCondition);
        request.setAttribute("condition",queryCondition);
        PageBean<User> page = adminService.getUserByPage(pageConditionSql);
        mv.addObject("currentPage",pageConditionSql.getCurrentPage());
        mv.addObject("page",page);
        mv.addObject("role","游客");
        mv.setViewName("allUser");
        return mv;
    }
}
//1.怎么防止用户进行空查询
